<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;
// use User\Api\UserApi;
     
/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class PublicController extends \Think\Controller {
    public  $iInfo  = array();
    public  $oInfo  = array();

    function __construct(){
        parent::__construct();
        $post   = file_get_contents('php://input');
        $post   = (array)json_decode($post, true);
        $this->iInfo    = I('request.') + $post;
    }

    /**
     * 用户登录
     * @param   $username 用户名
     * @param   $password 密码
     * @param   $remeber_me 记住我
     */
    function login(){
        $this->unEmptyParam(array('username', 'password'));
        $username   = $this->iInfo['username'];
        $password   = $this->iInfo['password'];
        $password   = md5(C('DATA_AUTH_KEY').$password);
        $remeberMe  = $this->iInfo['remeber_me'];

        $userInfo   = D('User')->login($username, $password);
        if(!is_array($userInfo))
            $this->outOInfo($userInfo, 0);

        //是否记录登录
        if($remeberMe){
            $cUser  = array(
                'username'  => $username,
                'password'  => $password
            );
            safe_cookie('user_info', $cUser);
        }

        $this->outOInfo(array('info' => $userInfo));
    }

    /**
     * 非空参数检测
     * @param  arra $params 非空参数组
     * @return null         
     */
    function unEmptyParam($params){
        foreach($params as $param){
            if(!isset($this->iInfo[$param]))
                $this->outOInfo('参数'.$param.'未找到!', 0);
        }
    }

    /**
     * 退出登录
     * @return  $boolean [description]
     */
    public function logout(){
        $result     = D('User')->logout();
        $this->outOInfo('操作成功');
    }

    /**
     * 菜单获取接口
     * @return  $list [<description>]
     */
    public function getMenuList(){
        $menus  = D('Menu')->getTreeMenus();
        foreach($menus as $key => &$menu){

            if($menu['hide'] == 1){
                unset($menus[$key]);
                continue;
            }

            foreach($menu['child'] as $key2 => &$menu2){
                if($menu2['hide'] == 1){
                    unset($menu['child'][$key2]);
                    continue;
                }

                unset($menu2['child']);
            }
        }
        $this->outOInfo(array('menus' => $menus));
    }

    /**
     * 输出信息
     * @param   $info   输出信息设置   
     * @param   $code   错误码     0:成功
     */
    function outOInfo($data, $code = 1){
        $this->oInfo['code']    = $code;

        if($data){
            if(is_array($data)){
                $this->oInfo['data']    = array_merge((array)$this->oInfo['data'], $data);
            }
            if(is_string($data)){
                $this->oInfo['data']    = $data;
            }
        }else{
            $this->oInfo['data']    = C('ERROR_CODE.'.$code) || '未定义错误!';
        }
        
        echo json_encode($this->oInfo);
        exit;
    }

    /**
     * 文件上传
     * @param   $_FILES
     */
    function upload(){
        if(empty($_FILES))
            $this->outOInfo('File not find!', 0);

        $upload = new \Think\Upload();
        $upload->rootPath  =     'Uploads/';

        $fileInfo   =   $upload->upload();
        if(!$fileInfo)
            $this->outOInfo($upload->getError(), 0);

        //根据类型返回对应信息
        $type   = $this->iInfo['type'];
        switch($type){
            case 'wangeditor':
                $path   = C('SERVER_CDN').'/server/Uploads/'.
                    $fileInfo['Filedata']['savepath'].$fileInfo['Filedata']['savename'];
                echo $path;
                exit;
            break;
            default:
                $savePath['path']   = '/Uploads/'.$fileInfo['Filedata']['savepath'].$fileInfo['Filedata']['savename'];
                $savePath['name']   = $fileInfo['Filedata']['name'];
                $savePath['size']   = floor($fileInfo['Filedata']['size']/1024);
                $this->outOInfo(array('info' => $savePath));
            break; 
        }
            
    }

    function test(){

    }
}
